package com.winit.issue.eventListener;

import com.winit.common.CloudLinkSend.MqMessage;
import com.winit.common.CloudLinkSend.MqMsgType;
import com.winit.issue.manager.IssueManager;
import com.winit.issue.model.Issue;
import com.winit.issue.service.IIssueService;
import com.winit.need.eventListener.NeedListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by wanglin on 2018/5/31.
 */
@Component
@RabbitListener(queues = "issue-receive-status-queue")
public class IssueListener {
    private static Logger logger = LoggerFactory.getLogger(IssueListener.class);

    @Autowired
    IssueManager issueManager;
    @Autowired
    private IIssueService issueService;

    @RabbitHandler
    public void process(MqMessage message) {
        Map json = message.getMap();
        MqMsgType type = message.getType();
        logger.info(json.toString());
        switch (type){
            case PROJECT:
                List list = (List) json.get("reqIds");
                this.updateIssueStatusPlan(list);
                break;
            case DISSOCIATION:
                this.updateIssueStatusPrd((List) json.get("reqIds"));
                break;
            case ONLINE:
                this.issueService.operateForReqOnline((String) json.get("reqId"));
        }
    }
    //接收消息将用户故事修改为立项开发
    private void updateIssueStatusPlan(List<String> reqIds){
        for(String reqId:reqIds){
            List<Issue> issues = issueManager.getReqIssues(reqId);
            for(Issue issue:issues){
                if("PRD".equals(issue.getStatus())){
                    issue.setStatus("ISSUE_DEV");
                    issueManager.updateIssue(issue);
                }
            }
        }
    }
    //接收消息将用户故事修改为产品设计
    private void updateIssueStatusPrd(List<String> reqIds){
        for(String reqId:reqIds){
            List<Issue> issues = issueManager.getReqIssues(reqId);
            for(Issue issue:issues){
                if("ISSUE_DEV".equals(issue.getStatus())){
                    issue.setStatus("PRD");
                    issueManager.updateIssue(issue);
                }
            }
        }
    }
}
